package com.aya.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * @author Karen
 * 商城订单的延迟队列
 */
@Service
public class DelayOrderListener implements ChannelAwareMessageListener {
    Logger logger = LoggerFactory.getLogger(DelayOrderListener.class);


    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            String messageBody = new String(message.getBody(), StandardCharsets.UTF_8);
            logger.info("延迟队列获得的内容:{}，如果订单未支付就修改为取消支付",messageBody);
            Thread.sleep(2000);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (AmqpException e) {
            logger.error("接受消息出现错误",e);
            channel.basicReject(2L,false);
        }

    }
}
